import React from 'react'
import Admin from "../../../layout/Admin";
import WithdrawModel from "../../../model/withdraw";
import {Button, Checkbox, DatePicker, Form, Input, Modal, Radio, Select, Table} from "antd";
import PaginationFooter from "../../../component/PaginationFooter/PaginationFooter";
import {defaultEnd, defaultStart} from "../../user/user-total/user-total";
import moment from "moment";
import {formLayout, formTailLayout} from "../../operation/vote/vote";
import Info from "../../../service/info";
const log = console.log.bind(console)
const {RangePicker} = DatePicker


class Withdraw extends React.Component {
    constructor(props) {
        super(props)
        this.model = new WithdrawModel()
        this.state = {
            list: [],
            p: 0,
            type: -1,
            start: defaultStart,
            end: defaultEnd,
            timeCheck: false,
            record: null,
            modal: {
                set: false,
                check: false,
            }
        }
        this.columns = [
            // Uid         string
            {
                dataIndex: 'Uid',
                title: 'Uid',
            },
            // Quota       int64  //提现金额
            {
                dataIndex: 'Quota',
                title: '提现金额',
                render: (int) => (int / 100).toFixed(2)
            },
            // Realname    string //真实姓名
            {
                dataIndex: 'Realname',
                title: '真实姓名',
            },
            // Nickname    string //用户昵称
            {
                dataIndex: 'Nickname',
                title: '用户昵称',
            },
            // Createt     int64  //申请时间
            {
                dataIndex: 'Createt',
                title: '申请时间',
                render: (time) => moment(new Date(time * 1000)).format('YYYY-MM-DD HH:mm')
            },
            // Dealno      string //提现流水号
            {
                dataIndex: 'Dealno',
                title: '提现流水号',
            },
            // Zfbnumber   string //收款支付宝
            {
                dataIndex: 'Zfbnumber',
                title: '支付宝',
            },
            // Phonenumber string   //手机号
            {
                dataIndex: 'Phonenumber',
                title: '手机号',
            },

            // State       int    //审核状态,0=待受理,1=已拒绝,2=已通过
            {
                dataIndex: 'State',
                title: '审核状态',
                render: (state) => ['待受理', '已拒绝', '已通过',][state]
            },
            // Checkuser   string //审核操作人账号
            {
                dataIndex: 'Checkuser',
                title: '审核操作人账号',
            },
            // Checktime   int64  //受理时间
            {
                dataIndex: 'Checktime',
                title: '受理时间',
                render: (time) => (time === 0 ? '' : moment(new Date(time * 1000)).format('YYYY-MM-DD HH:mm'))
            },
            {
                dataIndex: 'Usercreatet',
                title: '用户账号创建时间',
                render: (time) => (time === 0 ? '' : moment(new Date(time * 1000)).format('YYYY-MM-DD HH:mm'))
            },
            {
                dataIndex: 'Userlastlogint',
                title: '用户账号最后登录时间',
                render: (time) => (time === 0 ? '' : moment(new Date(time * 1000)).format('YYYY-MM-DD HH:mm'))
            },
            // Txt         string //备注
            {
                dataIndex: 'Txt',
                title: '备注',
            },
            // tableName   struct{} `pg:"balanceapply"`
            {
                title: '操作',
                render: (record) => {
                    return <div className='action-links'>
                        <a onClick={() => this.callSetModal(record)}>修改</a>
                        {record.State === 0 && <a onClick={() => this.callCheckModal(record)}>审核</a>}
                    </div>
                }
            }
        ]
    }

    componentDidMount() {
        this.all()
    }

    all() {
        // Starttime int `binding:"min=0"`
        // Endtime   int `binding:"min=0"`
        // State     int `binding:"min=-1,max=2"` //0=待受理,1=已拒 绝,2=已通过
        // Dealno    string
        // Uid       string
        // P         int `binding:"min=0"`
        const params = {
            P: this.state.p,
            State: this.state.type,
        }

        if (this.state.timeCheck) {
            // 2006-01-02 15:04:05
            const start = parseInt(this.state.start.format('X'))
            const end = parseInt(this.state.end.format('X'))
            params.Starttime = start
            params.Endtime = end
        }
        this.model
            .all(params)
            .then(res => {
                const data = res.data.ok
                log(data)
                this.setState({
                    list: data,
                })
            })
    }

    check(values) {
        const params = {
            Dealno: this.state.record.Dealno,
            Username: localStorage.getItem('username'),
            State: values.State,
            Txt: values.Txt || '',
        }
        this.model.check(params)
            .then(res => {
                const data = res.data.ok
                if (data) {

                    this.checkForm.resetFields()
                    Info.success('审核成功')
                    this.setState({
                        record: null,
                    }, () => {
                        this.cancelModal('check')
                        this.all()
                    })
                }
            })
    }

    set(values) {
        const params = {
            ...values,
            Dealno: this.state.record.Dealno,
        }
        this.model.set(params)
            .then(res => {
                const data = res.data.ok
                if (data) {

                    this.setForm.resetFields()
                    Info.success('修改成功')
                    this.setState({
                        record: null,
                    }, () => {
                        this.cancelModal('set')
                        this.all()
                    })
                }
            })
    }

    download = () => {
        const params = {
            State: this.state.type,
            Starttime: 0,
            Endtime: 0,
        }
        if (this.state.timeCheck) {
            // 2006-01-02 15:04:05
            const start = parseInt(this.state.start.format('X'))
            const end = parseInt(this.state.end.format('X'))
            params.Starttime = start
            params.Endtime = end
        }
        log(params)
        this.model.download(params)
            // .then(res => {
            //     log(res.data.ok)
            // })
    }

    onSetFinished = (values) => {
        this.set(values)
    }

    onCheckFinished = (values) => {
        this.check(values)
    }

    callSetModal = (record) => {
        this.setState({
            record,
        }, () => {

            this.callModal('set')
            setTimeout(() => this.setForm.setFieldsValue(record),0)
        })
    }

    callCheckModal = (record) => {
        this.setState({
            record,
        }, () => this.callModal('check'))
    }

    callModal(key, callback) {
        this.setState({
            ...this.state,
            modal: {
                ...this.state.modal,
                [key]: true,
            }
        })
    }

    cancelModal(key) {
        this.setState({
            ...this.state,
            modal: {
                ...this.state.modal,
                [key]: false
            }
        })
    }

    prevPage = () => {
        this.setState({
            p: this.state.p - 1,
        }, () => this.all())
    }

    nextPage = () => {
        this.setState({
            p: this.state.p + 1,
        }, () => this.all())
    }

    onTypeSelect = (value, option) => {
        this.setState({
            type: value,
            p: 0,
        }, () => this.all())
    }

    onDateChange = (value,) => {
        const [start, end,] = value
        this.setState({
            start,
            end,
        }, () => {
            // this.get()
            this.all()
        })
    }

    render() {
        return (
            <>
                <Modal
                    visible={this.state.modal.check}
                    title='审核'
                    footer={null}
                    onCancel={() => this.cancelModal('check')}
                >
                    <Form
                        {...formLayout}
                        name='check'
                        ref={el => this.checkForm = el}
                        onFinish={this.onCheckFinished}
                    >
                        <Form.Item
                            name="State"
                            label="状态"
                            rules={[
                                {
                                    required: true,
                                    message: '请选择',
                                },
                            ]}
                        >
                            <Radio.Group>
                                <Radio value={1}>拒绝</Radio>
                                <Radio value={2}>通过</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item
                            label="商品名称"
                            name="Txt"
                        >
                            <Input placeholder='备注'/>
                        </Form.Item>
                        <Form.Item {...formTailLayout}>

                            <Button type="primary" htmlType="submit">
                                Submit
                            </Button>

                        </Form.Item>
                    </Form>
                </Modal>
                <Modal
                    visible={this.state.modal.set}
                    title='修改'
                    footer={null}
                    onCancel={() => this.cancelModal('set')}
                >
                    <Form
                        {...formLayout}
                        name='set'
                        ref={el => this.setForm = el}
                        onFinish={this.onSetFinished}
                    >
                        <Form.Item
                            name='Phonenumber'
                            label='电话号码'
                        >
                            <Input placeholder='输入新的手机号码'/>
                        </Form.Item>
                        <Form.Item
                            name='Realname'
                            label='真实姓名'
                        >
                            <Input placeholder='输入新的真实姓名'/>
                        </Form.Item>
                        <Form.Item
                            name='Zfbnumber'
                            label='支付宝'
                        >
                            <Input placeholder='支付宝账号'/>
                        </Form.Item>
                        <Form.Item {...formTailLayout}>

                            <Button type="primary" htmlType="submit">
                                Submit
                            </Button>

                        </Form.Item>
                    </Form>
                </Modal>
                <Admin>
                    <div className='list-container'>
                        <div style={{marginBottom: '8px',}}>
                            <Select
                                style={{marginRight: '8px', width: 200}}
                                defaultValue={this.state.type}
                                value={this.state.type}
                                onChange={(val, opt) => this.onTypeSelect(val, opt)}
                            >

                                <Select.Option value={-1}>所有</Select.Option>
                                <Select.Option value={0}>待受理</Select.Option>
                                <Select.Option value={1}>已拒绝</Select.Option>
                                <Select.Option value={2}>已通过</Select.Option>
                            </Select>
                            <Checkbox
                                checked={this.state.timeCheck}
                                onChange={() => {
                                    this.setState({
                                        timeCheck: !this.state.timeCheck,
                                    }, () => {
                                        this.all()
                                    })
                                }}
                            >
                                时间:
                            </Checkbox>
                            <RangePicker
                                showTime={{ format: 'HH:mm:ss' }}
                                format="YYYY-MM-DD HH:mm:ss"
                                onChange={this.onDateChange}
                                defaultValue={[
                                    this.state.start,
                                    this.state.end,
                                ]}
                                disabled={this.state.timeCheck === false}
                            />
                            <Button
                                onClick={this.download}
                            >
                                下载数据
                            </Button>
                        </div>
                        <Table
                            dataSource={this.state.list}
                            columns={this.columns}
                            pagination={false}
                            rowKey={(record, index) => index}
                        >

                        </Table>
                        <PaginationFooter
                            pageIndex={this.state.p}
                            prevPage={this.prevPage}
                            nextPage={this.nextPage}
                        />
                    </div>
                </Admin>
            </>

        )
    }
}

export default Withdraw